package com.yingpai.fitness.dvp_2_0.main.activity.fitness;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.adpter.food.DieCollocationParentRecyclerAdapter;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.food.DieCollocationBean;
import com.yingpai.fitness.dvp_2_0.main.widget.CustomLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class DietCollocationActivity extends CommonActivity implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.diet_collocation_rv_list)
    RecyclerView dietCollocationRvList;
    @BindView(R.id.swiperefresh)
    SwipeRefreshLayout swiperefresh;
    private DieCollocationParentRecyclerAdapter mAdapter;
    private List<DieCollocationBean.MapData.DetailListWithPageData.ListData> mList = new ArrayList<>();
    private int id;
    private int pageNum = 1;
    private int pageSize = 4;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.diet_collocation_activity);
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            id = bundle.getInt("id", -1);
            title = bundle.getString("title");
        }
        setTitle(title + "搭配推荐");
        swiperefresh.setOnRefreshListener(this);
        dietCollocationRvList.setLayoutManager(new CustomLinearLayoutManager(this.getApplicationContext()));
        mAdapter = new DieCollocationParentRecyclerAdapter(R.layout.die_collocation_list_item, this);
        dietCollocationRvList.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.setOnItemChildClickListener(this);

    }

    /**
     * 初始化数据
     */
    @Override
    protected void initData() {
        pageNum = 1;
        requestData();
    }

    private void requestData() {
        showDialog(this, null);
        HttpParams params = new HttpParams();
        params.put("id", id);
        params.put("pageNum", pageNum++);
        params.put("pageSize", pageSize);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.GET_FOOD_RECOMMEND, params, Url.GET_FOOD_RECOMMEND, new Callback<DieCollocationBean>() {
            @Override
            public void onSuccess(DieCollocationBean dieCollocationBean) {
                if (Constant.REQUEST_SUCCESS.equals(dieCollocationBean.getResult())) {
                    if (dieCollocationBean.getMap().getDetailListWithPage().getTotal()>0) {
                        empty.setVisibility(View.GONE);
                    } else {
                        empty.setVisibility(View.VISIBLE);
                    }
                    if (pageNum - 1 == 1) {
                        mAdapter.setNewData(dieCollocationBean.getMap().getDetailListWithPage().getList());
                    } else {
                        mAdapter.addData(dieCollocationBean.getMap().getDetailListWithPage().getList());
                    }
                    mAdapter.notifyDataSetChanged();


                    if (mAdapter.getData().size() >= dieCollocationBean.getMap().getDetailListWithPage().getTotal()) {
                        mAdapter.loadMoreEnd();
                    } else {
                        mAdapter.loadMoreComplete();
                        mAdapter.setEnableLoadMore(true);
                    }
                    swiperefresh.setRefreshing(false);
                } else {
                    if (pageNum - 1 == 1) {
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        empty.setVisibility(View.GONE);
                    }
                    showToast(dieCollocationBean.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (pageNum - 1 == 1) {
                    empty.setVisibility(View.VISIBLE);
                } else {
                    empty.setVisibility(View.GONE);
                }
                dismissDialog();
            }
        });
    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.die_collocation_cover_iv:
                Bundle bundle = new Bundle();
                bundle.putInt("id", mAdapter.getItem(position).getId());
                startActivity(DieCollocationDetailActivity.class, bundle);
                break;
        }
    }

    @Override
    public void onLoadMoreRequested() {
        requestData();
    }

    @Override
    public void onRefresh() {
        pageNum = 1;
        requestData();
    }

    @Override
    @OnClick({})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
        }
    }
}
